package com.winz.cultureapp.filter.activity;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.SelLocationAdapter;
import com.winz.cultureapp.adapter.xrcv.SelPxAdapter;
import com.winz.cultureapp.entity.LocationBean;
import com.winz.cultureapp.entity.ShanXuanBean;
import com.winz.cultureapp.entity.ZhiNengPxBean;
import com.winz.filterbar.FilterTab;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动列表筛选栏--排序选择
 */
public class PxPopupWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private View window;
    private Context context;
    private FilterTab flag ;
    private int position = -1;

    private SelPxAdapter mAdapter;

    private List<ZhiNengPxBean.DataBean.ListBean> mDatas = new ArrayList<>();

    private OnFinishListener listener;


    public PxPopupWindow(Context context, FilterTab flag){
        super(context);
        this.context = context;
        this.flag = flag;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        window = inflater.inflate(R.layout.pop_activity_px, null);


        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);

        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow的View
        this.setContentView(window);
        window.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        initviews(window);
        loadDatas();
    }



    private void initviews(View window) {
        ListView lv_data = window.findViewById(R.id.lv_px);

        mAdapter = new SelPxAdapter(context,true, R.layout.item_sel_px);

        lv_data.setAdapter(mAdapter);
        lv_data.setOnItemClickListener(this);
    }

    private void loadDatas() {
        mDatas.add(new ZhiNengPxBean.DataBean.ListBean("0","默认排序"));
        mDatas.add(new ZhiNengPxBean.DataBean.ListBean("1","最新发布"));
        mDatas.add(new ZhiNengPxBean.DataBean.ListBean("2","人气最高"));
        mAdapter.setDatas(mDatas);
    }

    public void dismiss(){
        super.dismiss();
    }

    public void showAsDropDown(View root){
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            root.getGlobalVisibleRect(visibleFrame);
            int height = root.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(root);
        // 每次打开恢复记录
        if(position != -1){
            mAdapter.changeSel(position);
        }else{
            mAdapter.cleanAllState();
        }


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int p, long l) {
        mAdapter.changeSel(p);
        if (listener != null){
            String data = mDatas.get(mAdapter.getLastSeletPosition()).name+","+mDatas.get(mAdapter.getLastSeletPosition()).id;
            // 记录选择位置
            position = mAdapter.getLastSeletPosition();
            listener.onFinish(flag,data);
        }
        dismiss();

    }

    public void setOnFinishListener(OnFinishListener listener){
        this.listener = listener;
    }

    public interface OnFinishListener{
        void onFinish(FilterTab flag, String data);
    }

    public FilterTab getFlag(){
        return flag;
    }

}
